package com.hudongwx.shansun.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.shansun.R;
import com.hudongwx.shansun.activity.SettingActivity;
import com.hudongwx.shansun.utlis.API;
import com.hudongwx.shansun.utlis.MyJSONobject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class Fragment5 extends Fragment {

    private GridView gridView;
    private MyGridViewAdapter myGridViewAdapter;
    private Button btn1;
    private View view;
    private ImageView iv1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_fragment5, container, false);
        findId();
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });


        RequestParams params = new RequestParams(API.fragmen_5_like());
        x.http().get(params, new Callback.CommonCallback<MyJSONobject>() {
            @Override
            public void onSuccess(MyJSONobject result) {
                JSONArray array = null;
                try {
                    array = result.getJSONArray("links");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                myGridViewAdapter = new MyGridViewAdapter(array);
                gridView.setAdapter(myGridViewAdapter);

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        return view;
    }

    private void findId() {
        gridView = (GridView) view.findViewById(R.id.fragment_5_gv);
        iv1 = (ImageView) view.findViewById(R.id.fragment_5_iv_1);
    }

    class MyGridViewAdapter extends BaseAdapter {

        private JSONArray arry;

        public MyGridViewAdapter(JSONArray arry) {
            this.arry = arry;
        }

        @Override

        public int getCount() {
            return arry.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return arry.getJSONObject(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            MyViewHolder mViewHolder;
            if (null == convertView) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_5_like, null);
                mViewHolder = new MyViewHolder();
                mViewHolder.iv = (ImageView) convertView.findViewById(R.id.fragment_5_like_iv);
                mViewHolder.tv = (TextView) convertView.findViewById(R.id.fragment_5_like_tv);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (MyViewHolder) convertView.getTag();
            }
            JSONObject json = null;
            try {
                json = arry.getJSONObject(position);
                x.image().bind(mViewHolder.iv, json.getString("icon"));
                mViewHolder.tv.setText(json.getString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return convertView;
        }
    }

    class MyViewHolder {
        ImageView iv;
        TextView tv;


    }
}
